package com.vertygoeclypse.multitimer;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.util.Log;

public class TaskFragment extends Fragment {
    private static final String TAG = TaskFragment.class.getSimpleName();
    String i;
    static int counter, validcounter;
    Bundle bundle;
    static interface TaskCallbacks {
        public void onPreExecute();
        public void onProgressUpdate(int timer);
        public void onCancelled();
        public void onPostExecute();
    }
    private TaskCallbacks mCallbacks;
    private DummyTask mTask;
    static boolean mRunning;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof TaskCallbacks)) {
            throw new IllegalStateException("Activity must implement the TaskCallbacks interface.");
        }
        mCallbacks = (TaskCallbacks) activity;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        bundle=getArguments();
        i = bundle.getString("SecValue");
        Log.i("VertygoEclypse - TaskFragment-onCreate", i);
        counter=Integer.parseInt(i);
        Log.i("VertygoEclypse - TaskFragment - onCreate", String.valueOf(counter).toString());
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        cancel();
    }
    public void start() {
        if (!mRunning) {
            Log.i("VertygoEclypse - TaskFragment - start", String.valueOf(counter).toString());
            mTask = new DummyTask();
            validcounter = counter;
            Log.i("VertygoEclypse - TaskFragment - validcounter", String.valueOf(validcounter).toString());
            mTask.execute();
            mRunning = true;
        } else{
            mTask.cancel(true);
        }
    }
    public void cancel() {
        if (mRunning) {
            mTask.cancel(false);
            mTask = null;
            mRunning = false;
        }
    }
    public boolean isRunning() {
        return mRunning;
    }
    private class DummyTask extends AsyncTask<Void, Integer, Void> {
        @Override
        protected void onPreExecute() {
            mCallbacks.onPreExecute();
            mRunning = true;

            Log.i("Vertygo Eclypse - AsyncTask - onPreExecute", i);
        }
        @Override
        protected Void doInBackground(Void... ignore) {

            Log.i("Vertygo Eclypse - AsyncTask - onPreExecute", String.valueOf(validcounter).toString());
            do {
                publishProgress(validcounter);
                SystemClock.sleep(1000);
                validcounter=validcounter-1;
                if(isCancelled()){
                    mTask.cancel(true);
                    break;
                }
            } while (validcounter>0);
            return null;
        }
        @Override
        protected void onProgressUpdate(Integer... timer) {
            mCallbacks.onProgressUpdate(timer[0]);
        }
        @Override
        protected void onCancelled() {
            mCallbacks.onCancelled();
            mRunning = false;
        }
        @Override
        protected void onPostExecute(Void ignore) {
            mCallbacks.onPostExecute();
            mRunning = false;
        }
    }
}